/*******************************************************************************************************/
//                                  INITIALIZATION HEADERFILE                                          //
//                                                                      2009.05.10                     //
//                                                                      MADE BY Byeongyeol Kim         // 
/*******************************************************************************************************/
#include <iom128.h>
#include "HK_init.h"

void Port_Initialization()
{
      DDRA  = 0xff;
      DDRB  = 0xff;    // 1111 1111
      DDRC  = 0xff;
      DDRD  = 0xff;
      DDRE  = 0xfe;
      DDRF  = 0xff;
      DDRG  = 0xff;
      
      PORTA = 0xff;
      PORTB = 0x0f;    // 1111 1111
      PORTC = 0xff; 
      PORTD = 0x00;
      PORTE = 0x86;   //1000 0110
      PORTF = 0xff;
      PORTG = 0xff;
}

void External_Interrupt_Initialization()
{
      EIMSK = 0xff;      // 0.1,2,3,4,5,6,7 enable
      EICRA = 0xaa;      // 1010 1010  falling edge
      EICRB = 0xaa;      // 1010 1010  falling edge
      EIFR  = 0x00;
}



/*******************************************************************************************************/
//                                     DELAY FUNCTION                                                  //
/*******************************************************************************************************/
#define F_CPU 16000000  	                      //16.000MHz processor 250nS  CPU Clock Speed
#define CYCLES_PER_US  ((F_CPU+500000)/1000000)       //cpu cycles per microsecond

void delay_us(unsigned int us)
{
  unsigned int delay_loops;
  unsigned int i;
  delay_loops = (us+3)/5*CYCLES_PER_US;	              // +3 for rounding up
  for (i = 0; i < delay_loops; i++);	              // one loop takes 5 cpu cycles
}

void delay_ms(unsigned int d_cnt)
{
  unsigned int i;
  for (i = 0; i < d_cnt; i++)
  delay_us(1000);			              // 1000us => 1ms
}

